package org.mrzc.jedis;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.beanutils.BeanUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisException;

public final class RedisPool
{
    /**
     * 线程池的单例
     */
    private static JedisPool POOL = null;

    /**
     * 配置文件名
     */
    private static String PROFILE_PATH = "RedisConfig.properties";

    /**
     * 线程池是否有效的标志
     */
    private static boolean FLAG = true;

    /**
     * 获取Jedis实例
     * 
     * @return
     */
    public static Jedis getJedis()
    {
        if (!FLAG)
        {
            return null;
        }
        if (POOL == null)
        {
            synchronized (RedisPool.class)
            {
                if (POOL == null)
                {
                    initPool();
                }
            }
        }
        Jedis tJedis = null;
        try
        {
            tJedis = POOL == null ? null : POOL.getResource();
        } catch (JedisException e)
        {
            e.printStackTrace();
            synchronized (RedisPool.class)
            {
                FLAG = false;
            }
        }
        return tJedis;
    }

    /**
     * 重载线程池的配置文件
     */
    public static void Reload()
    {
        synchronized (RedisPool.class)
        {
            initPool();
        }
    }

    /**
     * 初始化线程池
     */
    private static void initPool()
    {
        InputStream tIn = RedisPool.class.getResourceAsStream(PROFILE_PATH);
        Properties tProperties = new Properties();
        try
        {
            tProperties.load(tIn);
            JedisPoolConfig tPoolConfig = new JedisPoolConfig();
            Iterator<Entry<Object, Object>> tIterator = tProperties.entrySet().iterator();
            while (tIterator.hasNext())
            {
                Entry<Object, Object> entry = tIterator.next();
                String key = entry.getKey().toString();
                Object value = entry.getValue();
                BeanUtils.setProperty(tPoolConfig, key, value);
            }
            POOL = new JedisPool(tPoolConfig, tProperties.getProperty("host"),
                    Integer.parseInt(tProperties.getProperty("port")),
                    Integer.parseInt(tProperties.getProperty("timeout")),
                    tProperties.getProperty("password"));
            FLAG = true;
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        Jedis jedis = null;
        try
        {
            jedis = RedisPool.getJedis();
            if (jedis == null)
            {
                return;
            }
            System.out.println(jedis.hashCode());

        } finally
        {
            if (jedis != null)
            {
                jedis.close();
            }
        }
    }
}
